﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DAL
{
    public class GetVCode
    {
        static Random random = new Random();
        private static String randomCode;

        public static String RandomCode
        {
            get { return randomCode; }
            set { randomCode = value; }
        }

        /// <summary>
        /// 创建随机字符
        /// </summary>
        public static void GetRandomCode()
        {
            String AllCode = "";
            RandomCode = "";
            for (int i = 0; i < 10; i++)
            {
                AllCode += i;
            }
            for (int i = 65; i < 91; i++)
            {
                AllCode += (char)i;
            }
            for (int i = 97; i < 123; i++)
            {
                AllCode += (char)i;
            }

            for (int i = 0; i < 4; i++)
            {
                RandomCode += AllCode.ToCharArray()[random.Next(AllCode.Length)].ToString();
            }
           
        }

        public static Bitmap CreateImage(PictureBox pictureBox,int fontSize)
        {

            int width = (RandomCode.Length * fontSize) + 40;
            int height = fontSize * 2 + 10;
            Bitmap bitmap = new Bitmap(width, height);
            Graphics graphics = Graphics.FromImage(bitmap);
            graphics.Clear(Color.White);
            Font font = new Font("微软雅黑", 16, FontStyle.Bold);
            graphics.DrawString(RandomCode,font, new SolidBrush(Color.Black), 10, 10);

            for (int i = 0; i < 50; i++)
            {
                int x1 = random.Next(pictureBox.Width);
                int x2 = random.Next(pictureBox.Width);

                int y1 = random.Next(pictureBox.Height);
                int y2 = random.Next(pictureBox.Height);

                graphics.DrawLine(new Pen(Color.FromArgb(random.Next()), 2), x1, y1, x2, y2);
            }
            return bitmap;
        }




    }
}
